#include "sampleserver.h"



SampleServer::SampleServer() {	
    ui = new Ui_SampleServer();
    ui->setupUi(this);
	ui->console->setAutoFillBackground(true);
    connect(ui->powerButton, SIGNAL(clicked()),
		this, SLOT(powerSwitch()));

		 selectDialog = new SelectCallDataDialog();
selectDialog->showList("default", "aud", 
			"a.aud", "random");
		selectDialog->show();

	syncManager = new SyncManager();
	//phoneBookDialog = NULL;
	updateStatus(OFF);
	connect(this, SIGNAL(addContactSignal(NeoPhoneBookEntry*)),
			syncManager, SLOT(addContactListener(NeoPhoneBookEntry*)));
	connect(this, SIGNAL(replaceContactSignal(int , NeoPhoneBookEntry*)),
			syncManager, SLOT(replaceContactListener(int, NeoPhoneBookEntry*)));
	connect(this, SIGNAL(deleteContactSignal(int)),
			syncManager, SLOT(deleteContactListener(int)));
	connect(this, SIGNAL(callContactSignal(int)),
			syncManager, SLOT(callContactListener(int)));
}

SampleServer::~SampleServer() {
    delete ui;
}

void SampleServer::powerSwitch() {
	if (powerOn) {	
		//===== Turn Off
		syncManager->stop();	
		updateStatus(OFF);
	} else {
		//===== Turn On
viewDialog=new ViewContactDialog(selectDialog);
NeoPhoneBookEntry *entry=new NeoPhoneBookEntry("sean","1234");
entry->setPictureFilePath("MichaelJordan.jpg");
viewDialog->setupView(entry);
viewDialog->show();
		//PhoneBookDialog *dialog = new PhoneBookDialog();
		//dialog->show();
		QString ipStr = ui->ipLineEdit->text();
		QString portStr = ui->portLineEdit->text();
		switch (syncManager->listen(ipStr, portStr)) {
		case SyncManager::LISTEN:
			updateStatus(ON);
			break;
		case SyncManager::ERROR_NETWORK:
			updateStatus(ERROR);
			break;
		}
    }
}


void SampleServer::phoneBookDialogCloseListener() {
}

void SampleServer::updateStatus(int status) {
	switch (status) {
	case ON:
		powerOn = true;
		ui->ipLineEdit->setEnabled(false);
		ui->portLineEdit->setEnabled(false);
		ui->statusLabel->setText("Listening...");
    	ui->console->setText("Waiting for connection");
		ui->powerButton->setText("stop");
		break;
	case OFF:
		powerOn = false;
		ui->ipLineEdit->setEnabled(true);
		ui->portLineEdit->setEnabled(true);
		ui->powerButton->setText("start");
		ui->console->setText("Server stoped.");
		break;
	case ERROR:
		powerOn = false;
		ui->console->setText("Cannot listen on the given address");
		break;
	case CONNECT:
		break;
	default:
		break;
	}
}


void SampleServer::on_addButton_clicked() {
	qDebug()<<"add button clicked";
	NeoPhoneBookEntry *entry = new NeoPhoneBookEntry();
	entry->setPhoneBookEntry("add|add|email|phone|Default|Pulse|Pulse|0|0");
	emit addContactSignal(entry);
}
void SampleServer::on_deleteButton_clicked() {
	qDebug()<<"delete button clicked";
	emit deleteContactSignal(0);
}
void SampleServer::on_replaceButton_clicked() {
	qDebug()<<"replace button clicked";
	NeoPhoneBookEntry *entry = new NeoPhoneBookEntry();
	entry->setPhoneBookEntry("replace|replace|email||Default|Pulse|Pulse|0|0");
	emit replaceContactSignal(0, entry);
}
void SampleServer::on_callButton_clicked() {
	qDebug()<<"call button clicked";
	emit callContactSignal(0);
}




